module cl {
	export class Interaction {

		public static directions : DirectionDO[] = [];
		public static direction : DirectionDO;

		public static init() : void {
			for (let i = 0; i < Campaign.force.length; i++) {
				Interaction.directions.push(new DirectionDO([], 0, Util.createArray(Campaign.troop.length, -1), Util.createArray(Campaign.troop.length, -1)));

				// 默认平衡模式: 1
				// for (let troopid of Campaign.force[i].courtTroop) {
				// 	Interaction.directions[i].strategies[troopid] = 1;
				// }
				for (let troopid of Campaign.force[i].innerTroop) {
					Interaction.directions[i].strategies[troopid] = 1;
				}
			}
		}

		public static status : StatusDO;

		public static selectingTroops : number[];

		public static selectingType : string;

		public static getSelectingType(troopid : number) : string {
			return Campaign.troop_plane[troopid] == 0 ? "0" : "1 " + Campaign.troop[troopid].type_name;
		}

		public static strategySymbol : string[] = ["☩", "∩", "≈"]; //"▲", "∧", "∨", "■", "△", "Ω" , "～", "Π", "∽", "□", "┿", "s", "S", "◎", "ʃ", "ㅠ"

		public static reInit() : void {
			Interaction.status = new StatusDO();
			Interaction.selectingTroops = [];
			Interaction.selectingType = "";
			Interaction.direction = Interaction.directions[Campaign.time_ref.force_of_step];
			Interaction.direction.regroups = Util.createArray(Campaign.troop.length, -1);
		}

		public static refreshSelectTroopStatus() : void {
			let selectInner : boolean = Interaction.selectingType[0] == "0";
			if (Interaction.selectingTroops.length == 0) {
				Interaction.status.popSameGroupStatusOf("select_one_inner_troop");
			} else if (Interaction.selectingTroops.length == 1) {
				Interaction.status.pushStatus(selectInner ? "select_one_inner_troop" : "select_one_court_troop");
			} else {
				Interaction.status.pushStatus(selectInner ? "select_multi_inner_troop" : "select_multi_court_troop");
			}
		}

		public static nextTeamName() : string {
			return String(++Interaction.direction.teamCharNumber);
		}

		public static findTeam(troopids : number[]) : number {
			for (let i = 0; i < Interaction.direction.teams.length; i++) {
				let team = Interaction.direction.teams[i];
				if (team.troopids.length == troopids.length){
					let allSame = true;
					for (let troopid of troopids) {
						if (team.troopids.indexOf(troopid) < 0) {
							allSame = false;
							break;
						}
					}
					if (allSame){
						return i;
					}
				}
			}

			return -1;
		}

		public static findTeam_troop(forceid : number, troopid : number) : number {
			for (let i = 0; i < Interaction.directions[forceid].teams.length; i++) {
				let team = Interaction.directions[forceid].teams[i];
				if (team.troopids.indexOf(troopid) >= 0){
					return i;
				}
			}

			return -1;
		}

		public static findRelatedTeams_inversed(troopids : number[]) : number[] {
			let modified : number[] = [];
			for (let i = Interaction.direction.teams.length - 1; i >= 0; i--) {
				let containsTroop = false;
				let team = Interaction.direction.teams[i];
				for (let troopid of troopids) {
					if (team.troopids.indexOf(troopid) >= 0) {
						containsTroop = true;
						break;
					}
				}
				if (containsTroop) {
					modified.push(i);
				}
			}
			return modified;
		}

		/** 删除原有的重组命令，返回被修改后需要重画符号的部队编号 */
		public static unlinkRegroup(troopid : number) : number {
			let modifiedTroop = -1;
			if (Interaction.direction.regroups[troopid] > 1000000) {
				// 被别的部队重组指向，则取消
				let tid_tmp = Interaction.direction.regroups[troopid] - 1000000;
				let troopid_tmp = Campaign.troop_map[tid_tmp];
				Interaction.direction.regroups[troopid_tmp] = -1;

				modifiedTroop = troopid_tmp;
			} else if (Interaction.direction.regroups[troopid] >= 0) {
				let tid_tmp = Interaction.direction.regroups[troopid];
				let troopid_tmp = Campaign.troop_map[tid_tmp];
				if (troopid_tmp >= 0) {
					// 原指向别的部队，则取消
					Interaction.direction.regroups[troopid_tmp] = -1;
				}
				modifiedTroop = troopid;
			}

			Interaction.direction.regroups[troopid] = -1;

			return modifiedTroop;
		}
	}
}